/*
 * @Copyright: Citylife 2021
 * @Author: shaokui.gu
 * @Date: 2021-02-19 14:22:53
 * @Description: 
 */
import 'package:get/get.dart';

import '../utils/network/network_exception.dart';
import '../utils/toast/toast_manager.dart';
import 'base.dart';

abstract class BaseController extends GetxController {
  final _isFetchingData = false.obs;
  bool get isFetchingData => this._isFetchingData.value;
  set isFetchingData(value) => this._isFetchingData.value = value;
}

abstract class TemplateController extends BaseController with NetworkDelegate {
  /// 发送请求进度
  final _sendRequestProgress = 0.0.obs;
  get sendRequestProgress => this._sendRequestProgress.value;
  set sendRequestProgress(value) => this._sendRequestProgress.value = value;

  /// 接收请求数据进度
  final _receiveRequestProgress = 0.0.obs;
  get receiveRequestProgress => this._receiveRequestProgress.value;
  set receiveRequestProgress(value) =>
      this._receiveRequestProgress.value = value;

  @override
  void onSendRequest(String key) {
    isFetchingData = true;
  }

  @override
  void onBusinessResult(result, String key, bool isFromCache,
      bool isLastRequest, NetworkAccessType networkAccessType) {
    if (isLastRequest) isFetchingData = false;
  }

  @override
  void onException(Exception exception, bool isLastRequest) {
    if (isLastRequest) isFetchingData = false;
    // TODO: implement onException
    if (exception is BusinessException) {
      /// 业务异常
      ToastManager.showToast(msg: exception.message);
    } else {
      /// 通用异常
      ToastManager.showToast(msg: exception.toString());
    }
  }

  @override
  void onSkipHandleResult(String key, bool isLastRequest) {
    // TODO: implement onSkipHandleResult
    if (isLastRequest) isFetchingData = false;
  }
}
